﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WoSoft.PACS.Data.CheckData;
using DevComponents.DotNetBar.Controls;
using System.Runtime.InteropServices;
using WoSoft.PACS.Win.Properties;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmCapture : WoSoft.PACS.Win.FrmBase
    {
        //检查的业务对象
        private CheckApp _checkapp = new CheckApp();

        //当前选择病人的申请ID
        private string appFormID = string.Empty;

        public FrmCapture(string appID)
        {
            InitializeComponent();
            appFormID = appID;
        }
           
        private void FrmCapture_Load(object sender, EventArgs e)
        {
            this.pacsCamera1.RegHotKey();//注册热键
            this.pacsCamera1.HidePanelImg();

            this.pacsCamera1.SetState(3);
            try
            {
                this.pacsCamera1.Play();//自动预览
            }
            catch
            { }

            //加载图像信息
            this.pacsCamera1.CleraImages();
            var imgList = this._checkapp.GetImage(this.appFormID);
            if (imgList != null && imgList.Count > 0)
            {
                foreach (var imgModel in imgList)
                {
                    if (imgModel.ImageBytes != null)
                    {
                        var bmp = ByteToImage(imgModel.ImageBytes);
                        this.pacsCamera1.AddImage(bmp, imgModel.ID);
                    }
                }
            }

            //加载采集图像事件
            this.pacsCamera1.onCameraing += new MyControls.PACSCamera.CameraingHandler(pacsCamera1_onCameraing);

            //加载图像删除事件
            this.pacsCamera1.onDeleteImg += new MyControls.PACSCamera.DeleteImgHandler(pacsCamera1_onDeleteImg);

            //双击图像
            this.pacsCamera1.onDbClickImage += new MyControls.PACSCamera.ImageDBClickHandler(pacsCamera1_onDbClickImage);

        }

        //采集图像事件
        void pacsCamera1_onCameraing(System.Drawing.Bitmap bmp, string fileName)
        {
            var captureImgsPath = Path.Combine(Application.StartupPath, "CaptureImages");
            var currentDay = DateTime.Now.ToString("yyyyMMdd");
            if (!Directory.Exists(captureImgsPath))
            {//路径不存在,创建根目录
                Directory.CreateDirectory(captureImgsPath);
            }

            //检查当前的目录是否存
            var imgSavePath = Path.Combine(captureImgsPath, currentDay);
            if (!Directory.Exists(imgSavePath))
            {
                Directory.CreateDirectory(imgSavePath);
            }

            var jpgfileName = Path.Combine(imgSavePath, fileName + ".jpg");
            if (bmp != null)
            {
                //保存物理文件
                //bmp.Save(jpgfileName);  
                var dat = ImageToByte(bmp);

                //保存图像(到数据库)
                //this._checkapp.SaveImage(fileName, this.appFormID, dat);

                //刷新主界面图像
                //SendMessageToMain("refresh");
                SendMessageToMain(fileName + "|" + jpgfileName);
            }
        }

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(
        int hWnd,  //  handle  to  destination  window  
        int Msg,  //  message  
        int wParam,  //  first  message  parameter  
        ref  COPYDATASTRUCT lParam  //  second  message  parameter  
        );

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);

        public delegate bool EnumWindowProc(int hWnd, int parameter);

        [DllImport("user32.dll", EntryPoint = "EnumChildWindows")]
        public static extern bool EnumChildWindows(int window, EnumWindowProc callback, int lParam);

        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        private static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);

        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        const int WM_COPYDATA = 0x004A;

        private void SendMessageToMain(string strMSG)//发送消息到Main平台    
        {
            int maindHwnd = FindWindow(null, "检查报告");//获得主窗体的句柄 
            if (maindHwnd != 0)
            {
                byte[] sarr = System.Text.Encoding.Default.GetBytes(strMSG);
                int len = sarr.Length;

                COPYDATASTRUCT cds;
                cds.dwData = (IntPtr)100;
                cds.lpData = strMSG;
                cds.cbData = len + 1;
                SendMessage(maindHwnd, WM_COPYDATA, 0, ref  cds);
            }
        }

        //图像删除事件(控件删除触发)
        void pacsCamera1_onDeleteImg(string fileName)
        {
            //数据库删除
            this._checkapp.DeleteImage(fileName);
        }

        //图像双击事件
        void pacsCamera1_onDbClickImage(Image img)
        {
            var frmImageEdit = new FrmImageEdit(img, appFormID);
            frmImageEdit.StartPosition = FormStartPosition.CenterScreen;
            frmImageEdit.ShowDialog(this);
        }

        private void FrmCapture_ResizeEnd(object sender, EventArgs e)
        {
            Settings.Default.Top = this.Top;
            Settings.Default.Left = this.Left;
            Settings.Default.Width = this.Size.Width;
            Settings.Default.Height = this.Size.Height;

            Settings.Default.Save();
        }

        private void FrmCapture_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.pacsCamera1.CloseCamera();
            this.pacsCamera1.UnRegHotKey();//取消热键
        }
    }
}
